<?php

/**
 * 平台  　控制器
 *
 * @author liminghui
 */

require_once 'CommonController.php';

class Admin_ProductPlatformController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->view->title = "管理中心";
    }

    /**
     * 平台 信息管理（mob_partner_product_platform）
     * 
     * @return void
     */
    public function listAction()
    {
        $partnerProductModel = new Application_Model_PartnerProduct();//产品
        $productPlatformModel = new Application_Model_ProductPlatform();//平台

        $vars['backUrl'] = trim($this->_request->get('backUrl'));
        $vars['productCode'] = trim($this->_request->get('productCode')); 
        //取得mob_partner_product表里的产品信息
        if ($vars['productCode'] == '') {
            exit($this->view->message('请求的数据不正确！', $vars['backUrl']));    
        } else {
            $productRow = $partnerProductModel->fetchRow("code = '{$vars['productCode']}'");
            $this->view->productRow = $productRow;
        }

        $where = "WHERE ppp.`productCode` = '{$vars['productCode']}'";

        $sql = "SELECT ppp.*, pp.name AS productName FROM `{$productPlatformModel->getTableName()}` AS ppp "
            ."LEFT JOIN `{$partnerProductModel->getTableName()}` AS pp ON pp.`code` = ppp.`productCode` ";
        $sql .= $where;

        //分页
        $urls = $vars;
        $pageSize = 15;

        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();
        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        $row = $dbPaginator->getRows();
         //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $urls['pageNo'] = $pageNo;
        $urls_tmp = array();
        $urls_tmp['productCode'] = $urls['productCode'];
        $this->view->pageUrl = $this->view->projectUrl($urls_tmp) . urldecode('/backUrl' . $vars['backUrl']);
        $this->view->partners = $row;//数据库结果
        $this->view->vars = $vars;//参数
    }
    
    /**
     * 删除产品
     * 
     * @return void
     */
    public function deleteAction(){
        
        $productPlatformModel = new Application_Model_ProductPlatform();//平台
        $backUrl = trim($this->_request->get('backUrl'));//返回的url
        
        if ($this->_request->isPost()) {
            $groupIdArr = $this->_request->get('selectId');
            if (!empty($groupIdArr)) {
                foreach ($groupIdArr as $id) {
                    $row = $productPlatformModel->fetchRow("`id` = {$id}");
                    if (!empty($row)) {
                        $res = $productPlatformModel->delete("`id` = {$id}");
                        if(!$res){
                            exit($this->view->message('删除失败！',  $backUrl));
                        }
                    } else {
                        exit($this->view->message('数据不存在！',  $backUrl));
                    }
                }
            } else {
                exit($this->view->message('请选择相应参数！',  $backUrl));
            }
            
        } else {
            $id = trim($this->_request->get('id'));
            $row = $productPlatformModel->fetchRow("`id` = {$id}");
            if (!empty($row)) {
                $res = $productPlatformModel->delete("`id` = {$id}");
                if (!$res) {
                    exit($this->view->message('删除失败！', $backUrl));
                }
            } else {
                exit($this->view->message('数据不存在！', $backUrl));
            }
        }
        
        exit($this->view->message('删除成功！', $backUrl));
        
    }

    /**
     * 修改平台信息
     * 
     * @return void
     */
    public function updateAction()
    {
        $platformArr = $this->_configs['project']['platform'];
        $this->view->platformArr = $platformArr;

        $partnerProductModel = new Application_Model_PartnerProduct();
        $productPlatformModel = new Application_Model_ProductPlatform();//平台
        //取得参数
        $vars['partnerCode']     = trim($this->_request->getPost('partnerCode')) ?
            trim($this->_request->getPost('partnerCode')) : trim($this->_request->get('partnerCode'));  
        $vars['productCode']     = trim($this->_request->getPost('productCode')) ?
            trim($this->_request->getPost('productCode')) : trim($this->_request->get('productCode'));  
        $vars['id']     = trim($this->_request->getPost('id')) ?
            trim($this->_request->getPost('id')) : trim($this->_request->get('id'));  
        $vars['osName']     = trim($this->_request->getPost('osName')) ?
            trim($this->_request->getPost('osName')) : trim($this->_request->get('osName')); 
        $vars['notes']     = trim($this->_request->getPost('notes')) ?
            trim($this->_request->getPost('notes')) : trim($this->_request->get('notes')); 
        $vars['backUrl']     = trim($this->_request->getPost('backUrl')) ?
            trim($this->_request->getPost('backUrl')) : trim($this->_request->get('backUrl'));  
            
        //取得partner_product表里的商家信息
       if (empty($vars['partnerCode']) || empty($vars['productCode'])) {
                exit($this->view->message('请求的数据不正确！', $vars['backUrl'] ));    
       }

        //提交表单的处理　
        if ($this->_request->isPost()) {
           $id = $this->_request->getPost('id');//若不为空由用于更新，若为空则为添加
           $filter = new Zend_Filter_StripTags();
           $params = array(
            'partnerCode' => $filter->filter(trim($vars['partnerCode'])),
            'productCode' => $filter->filter(trim($vars['productCode'])),
            'osName' => $filter->filter(trim($this->_request->getPost('osName'))),
            'addTime' => date('Y-m-d H:i:s', time()),
            'notes' => $filter->filter(trim($this->_request->getPost('notes'))),
            );
            
           if ( !empty( $id )) {//更新
               
               //判读数据是否唯一　(productCode 和　osName　)
               $platformExist = $productPlatformModel->fetchRow("`osName` = '{$vars['osName']}' AND `productCode` = '{$vars['productCode']}' AND `id` != {$id}");
               if ( !empty( $platformExist ) ) {
                   exit($this->view->message('对应的操作系统已经存在！', $vars['backUrl']));    
               }
               unset($params['partnerCode']);
               unset($params['productCode']);
               unset($params['addTime']);
               $productPlatformModel->update($params, "`id` = {$id}");
               exit($this->view->message('更新成功！', urldecode($vars['backUrl'])));
           } else {
               
               //判读数据是否唯一　(productCode 和　osName　)
               $platformExist = $productPlatformModel->fetchRow("`osName` = '{$vars['osName']}' AND `productCode` = '{$vars['productCode']}'");
               if (!empty($platformExist)) {
                   exit($this->view->message('对应的操作系统已经存在！', $vars['backUrl']));    
               }
               $inser_id = $productPlatformModel->insert($params);
               exit($this->view->message('添加成功！', urldecode($vars['backUrl'])));
           }
        }
        
        //用于更新数据
        if (!empty($vars['id'])) {
            $platformUpdateInfo = $productPlatformModel->fetchRow("`id` = {$vars['id']}");
            if (empty($platformUpdateInfo)) {
                exit($this->view->message('此条记录不存在！', $vars['backUrl']));    
            }
            $vars['osName'] = $platformUpdateInfo['osName'];
            $vars['notes'] = $platformUpdateInfo['notes'];
        }
         $this->view->vars = $vars;//参数
    }

    /**
     * 添加平台信息
     * 
     * @return void
     */
    public function insertAction()
    {
        $platformArr = $this->_configs['project']['platform'];
        $this->view->platformArr = $platformArr;
        $partnerProductModel = new Application_Model_PartnerProduct();
        $productPlatformModel = new Application_Model_ProductPlatform();//平台
        //取得参数
        $vars['partnerCode']     = trim($this->_request->getPost('partnerCode')) ?
            trim($this->_request->getPost('partnerCode')) : trim($this->_request->get('partnerCode'));  
        $vars['productCode']     = trim($this->_request->getPost('productCode')) ?
            trim($this->_request->getPost('productCode')) : trim($this->_request->get('productCode'));  
        $vars['osName']     = trim($this->_request->getPost('osName')) ?
            trim($this->_request->getPost('osName')) : trim($this->_request->get('osName')); 
        $vars['notes']     = trim($this->_request->getPost('notes')) ?
            trim($this->_request->getPost('notes')) : trim($this->_request->get('notes')); 
        $vars['backUrl']     = trim($this->_request->getPost('backUrl')) ?
            trim($this->_request->getPost('backUrl')) : trim($this->_request->get('backUrl'));  
            
        //取得partner_product表里的商家信息
       if (empty($vars['partnerCode']) || empty($vars['productCode'])) {
                exit($this->view->message('请求的数据不正确！', $vars['backUrl'] ));    
       }

       //提交表单的处理　
       if ($this->_request->isPost()) {
           $filter = new Zend_Filter_StripTags();
           $params = array(
            'partnerCode' => $filter->filter(trim($vars['partnerCode'])),
            'productCode' => $filter->filter(trim($vars['productCode'])),
            'osName' => $filter->filter(trim($this->_request->getPost('osName'))),
            'addTime' => date('Y-m-d H:i:s', time()),
            'notes' => $filter->filter(trim($this->_request->getPost('notes'))),
            );
            
               
           //判读数据是否唯一　(productCode 和　osName　)
           $platformExist = $productPlatformModel->fetchRow("`osName` = '{$vars['osName']}' AND `productCode` = '{$vars['productCode']}'");
           if (!empty($platformExist)) {
               exit($this->view->message('对应的操作系统已经存在！', $vars['backUrl']));    
           }
           $inser_id = $productPlatformModel->insert($params);
           exit($this->view->message('添加成功！', urldecode($vars['backUrl'])));
        }
        
        $this->view->vars = $vars;//参数
    }
}